package tcp.longconnection.client.protocol;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public abstract class BasicProtocol {

	public static final int VERSION_LEN = 2; //协议的版本长度
	public static final int COMMEND_LEN = 4; //协议的类型: 0000心跳  0001普通文字聊天	0002服务器返回协议 0003好友列表请求	0004用户注册协议连接
	public static final int SLEFUUID_LEN = 32;	//自己的会话标识
	
	public static String VERSION = "00";
	
	public static String parseCommecnd(byte[] data) throws UnsupportedEncodingException {
		return new String(data, VERSION_LEN, COMMEND_LEN, "UTF-8");
	}
	
	public abstract String getCommand();
	
	public byte[] getContentData() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream(VERSION_LEN + COMMEND_LEN);
		bos.write(VERSION.getBytes(), 0, VERSION_LEN);
		bos.write(getCommand().getBytes(), 0, COMMEND_LEN);
		return bos.toByteArray();
	}
	
	public int parseBinary(byte[] data) {
		String version = new String(data, 0, VERSION_LEN);
		if(!"00".equals(version)) {
			throw new RuntimeException();
		}
		return VERSION_LEN + COMMEND_LEN;
	}
}
